//
//  XSQVisitorView.swift
//  WeiBo
//
//  Created by 许世强 on 2017/3/28.
//  Copyright © 2017年 许世强. All rights reserved.
//

import UIKit
import SnapKit

class XSQVisitorView: UIView {

  //重写initWithFrame方法
    override init(frame:CGRect){
        super.init(frame: frame)
        setupUI()
        
        
    }
    
    //定义点击注册和登录的闭包
    var loginClosure:(()->())?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //创建懒加载旋转视图
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    
    //创建罩层
    private lazy var makeIconImageView: UIImageView = UIImageView(image:UIImage(named:"visitordiscover_feed_mask_smallicon"))
    
    //创建主页(小房子)
     private lazy var homeImageView: UIImageView = UIImageView(image:UIImage(named:"visitordiscover_feed_image_house"))

    
    //访客视图信息  使用闭包
    private lazy var messageLabel : UILabel = {
    
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        label.textAlignment = .center
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        //  多行显示
        label.numberOfLines = 0
        return label
    }()
    
    //创建注册按钮
    private lazy var registerBtn: UIButton = {
    
        let button = UIButton()
        
        button.setTitle("注册", for: .normal)
        
        button.setTitleColor(UIColor.darkGray, for: .normal)
        
        button.setTitleColor(UIColor.orange, for: .highlighted)
        
      
        
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        button.adjustsImageWhenHighlighted = false
        
        let image = UIImage(named: "common_button_white_disable")?.resizableImage(withCapInsets: UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1), resizingMode: UIImageResizingMode.stretch)
        
          button.setBackgroundImage(image, for: .normal)
        
        //给注册按钮添加点击事件
        button.addTarget(self, action: #selector(registerANDloginAction), for: .touchUpInside)
        
        return button
        
    }()
    
    //创建登录按钮
    private lazy var loginButton: UIButton = {
    
        let button = UIButton()
    
        button.setTitle("登录", for: .normal)
        
        button.setTitleColor(UIColor.darkGray, for: .normal)
        
        button.setTitleColor(UIColor.orange, for: .highlighted)
        
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        let image =  UIImage(named: "common_button_white_disable")?.resizableImage(withCapInsets: UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1), resizingMode: UIImageResizingMode.stretch)
        
        button.setBackgroundImage(image, for: .normal)
        //  取消高亮效果
        button.adjustsImageWhenHighlighted = false
        
        //拉伸
       
        
        //给登录按钮添加点击事件
        button.addTarget(self, action: #selector(registerANDloginAction), for: .touchUpInside)
        
        return button
    }()
    
    //注册和登录按钮的点击事件
    @objc private func registerANDloginAction(){
    
        //点击事件中回调闭包
        loginClosure?()
    }
    
    //给控件设置约束
    private func setupUI(){
        
        backgroundColor = UIColor(white:237.0 / 255.0, alpha : 1)
        
        //在这里用到才创建
        addSubview(cycleImageView)
        addSubview(makeIconImageView)
        addSubview(homeImageView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginButton)
        
        cycleImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        makeIconImageView.snp.makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        
        homeImageView.snp.makeConstraints { (make) in
            make.center.equalTo(makeIconImageView)
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(homeImageView)
            make.top.equalTo(cycleImageView.snp.bottom)
            make.width.equalTo(224)
        }
        
        registerBtn.snp.makeConstraints { (make) in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerBtn)
            make.size.equalTo(registerBtn)
        }
    }
    
    //开启旋转动画
    private func startAnimation(){
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        
        animation.duration = 20
        
        animation.repeatCount = MAXFLOAT
        
        //动画失去焦点
        animation.isRemovedOnCompletion = false
        
        cycleImageView.layer.add(animation, forKey: nil)
    }
    
    //根据子控制器,修改访问视图信息
    func updataVisitorInfo(imageName: String?, message: String?){
    
        if let imageName = imageName , let message = message {
        
            homeImageView.image = UIImage(named: imageName)
            messageLabel.text = message
            
            //隐藏旋转视图
            cycleImageView.isHidden = true
        } else {
        
            startAnimation()
        }
        
       
    }
    
}
